package com.xca.is.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.xca.is.R;
import com.xca.is.databinding.ActivityYellowDateBinding;
import com.xca.is.entity.YellowDate;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.YellowDateModel;
import com.xca.is.utils.TimeUtils;
import com.xca.is.utils.WebViewScriptUtils;

public class YellowDateActivity extends AppCompatActivity implements HttpConnect {
    private ActivityYellowDateBinding binding ;

    private ViewModelProvider provider ;
    private YellowDateModel model ;
    private MutableLiveData<YellowDate> liveData ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityYellowDateBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.yeLoading.rLoading.setVisibility(View.GONE);

        int[] ints = new int[]{10,18};
        WebViewScriptUtils.htmlContent(ints,binding.yellowWeb);

        provider = new ViewModelProvider(this) ;
        model = provider.get(YellowDateModel.class);
        model.setModel(this);

        liveData = (MutableLiveData<YellowDate>) model.getLiveData() ;
        liveData.observe(this, new Observer<YellowDate>() {
            @Override
            public void onChanged(YellowDate yellowDate) {
                if (yellowDate!=null){
                    showData(yellowDate);
                }
            }
        });
        onClickListener();
        String time = TimeUtils.getDate() ;

        binding.date2.setText(TimeUtils.getDate());
        model.getData(TimeUtils.getDate());
    }


    private void onClickListener(){
        binding.date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDateChange();
            }
        });
        binding.date3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = binding.date2.getText().toString().trim() ;
                if (!date.isEmpty()){
                    model.getData(date);
                }else {
                    binding.date2.setError("请输入日期");
                }
            }
        });
    }


    private void showData(YellowDate yellowDate){
        YellowDate.ResultDTO data = yellowDate.getResult() ;
        binding.date4.setText(data.getGregoriandate());
        binding.date5.setText(data.getLunardate());
        binding.date6.setText(data.getLunar_festival());
        binding.date7.setText(data.getFestival());
        binding.date8.setText(data.getFitness());
        binding.date9.setText(data.getTaboo());
        binding.date10.setText(data.getShenwei());
        binding.date11.setText(data.getTaishen());
        binding.date12.setText(data.getChongsha());
        binding.date13.setText(data.getSuisha());
        binding.date14.setText(data.getWuxingjiazi());
        binding.date15.setText(data.getWuxingnayear());
        binding.date16.setText(data.getWuxingnamonth());
        binding.date17.setText(data.getXingsu());
        binding.date18.setText(data.getPengzu());
        binding.date19.setText(data.getJianshen());
        binding.date20.setText(data.getTiangandizhiyear());
        binding.date21.setText(data.getTiangandizhimonth());
        binding.date22.setText(data.getTiangandizhiday());
        binding.date23.setText(data.getLmonthname());
        binding.date24.setText(data.getShengxiao());
        binding.date25.setText(data.getLubarmonth());
        binding.date26.setText(data.getLunarday());
        binding.date27.setText(data.getJieqi());
    }

    private void showDateChange(){

        LayoutInflater inflater = getLayoutInflater() ;
        View view = inflater.inflate(R.layout.time_alert,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view) ;

        DatePicker datePicker = view.findViewById(R.id.datePicker);

        AlertDialog alertDialog = builder.create() ;
        datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                String date = i+"-"+(i1+1)+"-"+i2 ;
                binding.date2.setText(date);
            }
        });

        alertDialog.show();
    }

    @Override
    public void Loading(boolean loading) {
        binding.yeLoading.rLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        Toast.makeText(this, "请求超时", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}